﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Web;
using System.Xml.Linq;

using ProjectBase.Core;
using ProjectBase.Core.Dao;

namespace $safeprojectname$.Web
{
    /// <summary>
    ///     Web session implementation of the session storage
    /// </summary>
    public class WebSessionStorage : INHRequestSession, IDbRequestSession, IXmlRequestSession
    {
        #region Nested type: Keys
        class Keys
        {
            internal const string DbKey = Prefix + "DbKey";
            internal const string DbTransactionKey = Prefix + "DbTransactionKey";
            internal const string NHibernateKey = Prefix + "NHibernateKey";
            const string Prefix = "ProjectBase.Data.Sessions.WebSessionStorage.";
            internal const string XmlKey = Prefix + "XmlKey";
            internal const string XmlTransactionKey = Prefix + "XmlTransactionKey";
            protected Keys() { }
        }
        #endregion

        #region NHibernate
        /// <summary>
        ///     Returns ISession object saved in HttpContext.Item.
        ///     Appends watch to EndRequest event
        ///     to correctly close the ISession
        /// </summary>
        public virtual object GetSession(string name)
        {
            var context = HttpContext.Current;
            var session = context.Items[Keys.NHibernateKey + name];

            return session;
        }
        public virtual void SetSession(string name, object session)
        {
            var context = HttpContext.Current;

            if (session.IsNull())
            {
                context.Items.Remove(name);
                return;
            }
            context.Items[Keys.NHibernateKey + name] = session;
        }
        #endregion NHibernate

        #region Db
        /// <summary>
        ///     One IDbCommand for all Dao stuff during one request
        /// </summary>
        public virtual IDbCommand Command
        {
            get
            {
                var context = HttpContext.Current;
                var command = context.Items[Keys.DbKey] as IDbCommand;
                return command;
            }
            set
            {
                var context = HttpContext.Current;
                context.Items[Keys.DbKey] = value;
            }
        }

        /// <summary>
        ///     Allowes to create command with opened transaction if needed
        /// </summary>
        public virtual bool OpenTransaction
        {
            get
            {
                var context = HttpContext.Current;
                var isTransaction = context.Items[Keys.DbTransactionKey] as bool?;
                return isTransaction ?? false;
            }
            set
            {
                var context = HttpContext.Current;
                context.Items[Keys.DbTransactionKey] = value;
            }
        }
        #endregion Db

        #region Xml
        /// <summary>
        ///     Temporarily persisted collection of XDocuments
        /// </summary>
        public virtual IDictionary<string, XDocument> XDocuments
        {
            get
            {
                var context = HttpContext.Current;
                var xDocuments = context.Items[Keys.XmlKey] as IDictionary<string, XDocument>;
                return xDocuments;
            }
            set
            {
                var context = HttpContext.Current;
                context.Items[Keys.XmlKey] = value;
            }
        }

        /// <summary>
        ///     Keeps the information about Transaction - when needed
        /// </summary>
        public virtual bool IsTransaction
        {
            get
            {
                var context = HttpContext.Current;
                var isTransaction = context.Items[Keys.XmlTransactionKey] as bool?;
                return isTransaction ?? false;
            }
            set
            {
                var context = HttpContext.Current;
                context.Items[Keys.XmlTransactionKey] = value;
            }
        }
        #endregion Xml
    }
}